package me.spring.cloud.common.components.util.lang;

import cn.hutool.core.lang.WeightRandom;
import cn.hutool.core.lang.WeightRandom.WeightObj;
import cn.hutool.core.util.RandomUtil;
import java.util.ArrayList;
import java.util.List;

public class WeightRandomUtil {
  public static <T extends Weight> T weightRandom(List<T> list){
    List<WeightObj<Boolean>> weightList = new ArrayList<WeightRandom.WeightObj<Boolean>>();
    for (int i = 0; i < list.size(); i++) {
      weightList.add(new WeightObj(list.get(i),list.get(i).getWeight()));
    }
    WeightRandom wr = RandomUtil.weightRandom(weightList);
    return (T) wr.next();
  }
}
